package com.woniu.backstage.controller;


import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.woniu.backstage.common.PageResult;
import com.woniu.backstage.common.Result;
import com.woniu.backstage.entity.TbInform;
import com.woniu.backstage.entity.TbUser;
import com.woniu.backstage.entity.form.InformForm;
import com.woniu.backstage.service.ITbInformService;
import com.woniu.backstage.service.ITbUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author spirit
 * @since 2020-04-18
 */
@RestController
@RequestMapping("/backstage/inform")
public class TbInformController extends BaseController {
    @Autowired
    private ITbInformService informService;
    @Autowired
    private ITbUserService userService;

    @RequestMapping("/informShow")
    public String getInform(int page,int limit){
        int pageSize = limit;
        PageResult result = informService.getAllInform(page, pageSize);
        return JSON.toJSONString(result);
    }


    @RequestMapping("/pageLimit")
    public Result pageLimit(Integer page, Integer limit, InformForm informForm){
        System.out.println("informForm:"+informForm);
        Page<TbInform> informPage = new Page<>(page, limit);
        IPage<TbInform> informIPage = informService.pageLimit(informPage, informForm);
        //获取查询的数据
        List<TbInform> informs = informIPage.getRecords();
        System.out.println("informs:"+informs);
        //获取总的数据条数
        long total = informIPage.getTotal();
        return Result.success(informs, (int) total);
    }

    @RequestMapping("/addInform")
    public Result addInform(TbInform inform) throws Exception {
        List<TbUser> users = userService.selectAll();
        System.out.println("users长度"+users.size());
        int result = 0;
        for (TbUser user : users) {
            Long id = user.getId();
            inform.setUserId((id.intValue()));
            inform.setCreateTime(new Date());
            inform.setState(1);
            inform.setStatus(0);
            result = informService.addInform(inform);
        }
        if(result == 1){
            return Result.success();
        }
        throw new Exception("添加失败");
    }

    @RequestMapping("/deleteInform")
    public Result deleteInform(Integer id) throws Exception {
        int result = informService.deleteInform(id);
        if(result == 1){
            return Result.success();
        }
        throw new Exception("删除失败");
    }

    @RequestMapping("/deleteBatch")
    public Result deleteBatch(Integer[] idArr) throws Exception {
        Integer result = informService.deleteBatch(Arrays.asList(idArr));
        System.out.println(result);
        if(result > 0){
            return Result.success();
        }
        throw new Exception("删除失败");
    }

}

